Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Session

Public Class ManagePerformanceEvaluations

    Private Const DefaulGridViewRowHeight As Integer = 36

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim repository As New PerformanceEvaluationRepository()

        Dim evaluatorId As Guid = ClientSession.UserCredentials.Id

        Dim pe As IEnumerable(Of PerformanceEvaluation) = repository.GetAllByEvaluator(evaluatorId)

        Me.AddPerformanceEvaluations(pe)

    End Sub


    Public Sub AddPerformanceEvaluations(ByVal evaluations As IEnumerable(Of PerformanceEvaluation))

        For Each e As PerformanceEvaluation In evaluations
            AddPerformanceEvaluation(e)
        Next

    End Sub

    Private Sub AddPerformanceEvaluation(ByVal e As PerformanceEvaluation)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvPerformanceEvaluations)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.Id).Value = e.Id
        row.Cells(GridColumns.Evaluated).Value = e.Evaluated.Name
        row.Cells(GridColumns.StartDate).Value = e.StartDate
        row.Cells(GridColumns.EndDate).Value = e.EndDate

        grvPerformanceEvaluations.Rows.Add(row)

    End Sub

    Private Enum GridColumns
        Image = 0
        Id = 1
        Evaluated = 2
        StartDate = 3
        EndDate = 4
        Edit = 5
        Delete = 6
    End Enum

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Me.AddNewPerformanceEvaluations()
    End Sub

    Private Sub OnPerformanceEvaluationsAdded(ByVal evaluations As IEnumerable(Of PerformanceEvaluation))
        Me.AddPerformanceEvaluations(evaluations)
    End Sub

    Public Sub AddNewPerformanceEvaluations()

        Dim form As New CreatePerformanceEvaluation()
        AddHandler form.PerformanceEvaluationsAdded, AddressOf OnPerformanceEvaluationsAdded

        form.ShowDialog()

    End Sub

    Private Sub grvPerformanceEvaluations_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvPerformanceEvaluations.CellClick

        If e.RowIndex >= 0 Then

            Dim id As Guid = CType(grvPerformanceEvaluations.Rows(e.RowIndex).Cells(GridColumns.Id).Value, Guid)

            If e.ColumnIndex = GridColumns.Edit Then
                Me.EditPerformanceEvaluation(id)
            End If

            If e.ColumnIndex = GridColumns.Delete Then
                If UIHelper.DeletionConfirmed("Are you sure you want to delete this Performance Evaluation?") Then
                    If (Me.DeletePerformanceEvaluation(id)) Then
                        grvPerformanceEvaluations.Rows.Remove(grvPerformanceEvaluations.Rows(e.RowIndex))
                    End If
                End If
            End If

        End If

    End Sub

    Public Function DeletePerformanceEvaluation(ByVal id As Guid) As Boolean

        Dim peRepository As New PerformanceEvaluationRepository()

        peRepository.DeleteLogical(id)

        Return True

    End Function

    Public Sub EditPerformanceEvaluation(ByVal id As Guid)

        Dim form As New EditPerformanceEvaluation(id)

        form.ShowDialog()

    End Sub

    Private Sub grvPerformanceEvaluations_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvPerformanceEvaluations.CellMouseEnter
        If e.RowIndex >= 0 Then
            grvPerformanceEvaluations.Rows(e.RowIndex).Selected = True

            If e.ColumnIndex = GridColumns.Edit Or e.ColumnIndex = GridColumns.Delete Then
                Me.Cursor = Cursors.Hand
            End If

        End If
    End Sub

    Private Sub grvPerformanceEvaluations_CellMouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvPerformanceEvaluations.CellMouseLeave
        If e.RowIndex >= 0 Then
            grvPerformanceEvaluations.Rows(e.RowIndex).Selected = False
            Me.Cursor = Cursors.Arrow
        End If
    End Sub

    Private Sub btnCloseTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseTab.Click
        Me.Close()
    End Sub

End Class